﻿using System;
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;

namespace ZSW.Framework.Utils
{
    public static class ZSWFUtil_Texture
    {
        /// <summary>
        /// Texture2D转png图片
        /// </summary>
        public static void SaveAsPNG(Texture2D texture, string filePath)
        {
            byte[] bytes = texture.EncodeToPNG();
            File.WriteAllBytes(filePath, bytes);
        }

        /// <summary>
        /// png图片转Texture2D
        /// </summary>
        public static bool LoadPNG(string filePath, out Texture2D texture)
        {
            if (!ZSWFUtil_File.FileExists(filePath))
            {
                texture = null;
                return false;
            }

            byte[] fileData = File.ReadAllBytes(filePath);
            texture = new Texture2D(1, 1);
            texture.LoadImage(fileData);
            return true;
        }

        /// <summary>
        /// Texture2D转Sprite
        /// </summary>
        public static Sprite ConvertToSprite(Texture2D texture)
        {
            return Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
        }

        /// <summary>
        /// 根据网络链接获取图片
        /// </summary>
        public static IEnumerator DownloadTexture(string url, Action<Texture2D> callback)
        {
            using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(url))
            {
                yield return request.SendWebRequest();

                if (request.result == UnityWebRequest.Result.Success)
                {
                    Texture2D texture = DownloadHandlerTexture.GetContent(request);
                    callback?.Invoke(texture);
                }
                else
                {
                    Debug.Log($"Failed to download image from {url}: {request.error}");
                    callback?.Invoke(null);
                }
            }
        }
        
        /// <summary>
        /// 根据网络链接获取图片
        /// </summary>
        public static IEnumerator DownloadImage(string url, Action<Sprite> callback)
        {
            using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(url))
            {
                yield return request.SendWebRequest();

                if (request.result == UnityWebRequest.Result.Success)
                {
                    Texture2D texture = DownloadHandlerTexture.GetContent(request);
                    callback?.Invoke(ConvertToSprite(texture));
                }
                else
                {
                    Debug.Log($"Failed to download image from {url}: {request.error}");
                    callback?.Invoke(null);
                }
            }
        }
        
        /// <summary>
        /// 克隆Texture2D
        /// </summary>
        /// <param name="srcTexture"></param>
        /// <returns></returns>
        public static Texture2D Clone(Texture2D srcTexture)
        {
            // 获取源纹理的原始字节数据
            byte[] srcData = srcTexture.GetRawTextureData();
            // 复制源数据
            byte[] dstData = new byte[srcData.Length];
            System.Buffer.BlockCopy(srcData, 0, dstData, 0, srcData.Length);
            // 创建新纹理
            Texture2D dstTexture = new Texture2D(srcTexture.width, srcTexture.height, srcTexture.format, false);
            dstTexture.LoadRawTextureData(dstData);
            return dstTexture;
        }
        
        /// <summary>
        /// 克隆Texture2D
        /// </summary>
        /// <param name="srcTexture"></param>
        /// <returns></returns>
        public static Texture2D Clone(Texture srcTexture)
        {
            return Clone(srcTexture as Texture2D);
        }
        
        /// <summary>
        /// 将Texture2D转为RenderTexture格式
        /// </summary>
        /// <param name="texture"></param>
        /// <returns></returns>
        public static RenderTexture ToRenderTexture(Texture2D texture)
        {
            // 创建RenderTexture
            RenderTexture rt = new RenderTexture(texture.width, texture.height, 0);
            rt.enableRandomWrite = true;
            rt.Create();
            // 将Texture2D拷贝到RenderTexture
            Graphics.Blit(texture, rt);
            return rt;
        }
        
    }
}